package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.ArticleType;
import cn.itsource.query.ArticleTypeQuery;
import cn.itsource.service.IArticleTypeService;
import cn.itsource.util.R;
import cn.itsource.util.R;
import cn.itsource.vo.ArticleCategory;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    /**
     * 增加/修改
     * @param articleType
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(descs="文章类型增加或修改",name = "文章类型增加或修改",sn = "articleType:addORupdate")
    public R addORupdate(@RequestBody ArticleType articleType){
        try {
            if (articleType.getId() == null){
                articleTypeService.add(articleType);
            }else {
                articleTypeService.update(articleType);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(descs="文章类型删除",name = "文章类型删除",sn = "articleType:del")
    public R del(@PathVariable("id") Long id){
        try {
            articleTypeService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(descs="文章类型批量删除",name = "文章类型批量删除",sn = "articleType:delList")
    public R delList(@RequestBody List<Long> ids){
        try {
            articleTypeService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(descs="文章类型获取单个",name = "文章类型获取单个",sn = "articleType:getOne")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(articleTypeService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(descs="文章类型查询所有",name = "文章类型查询所有",sn = "articleType:getAll")
    public R getAll(){
        try {
            return R.ok(articleTypeService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(descs="文章类型分页查询",name = "文章类型分页查询",sn = "articleType:pageQuery")
    public PageInfo<ArticleType> pageQuery(@RequestBody ArticleTypeQuery query){
        return articleTypeService.pageQuery(query);
    }

    /**
     * 获取分类树
     * @return
     */
    @PatchMapping("/typeTrees")
    @ApiOperation(value = "获取分类树")
    @PreAuthorize(descs="文章类型获取分类树",name = "文章类型获取分类树",sn = "articleType:typeTrees")
    public R<List<ArticleType>> articleTypeTree(){
        try {
            return R.ok(articleTypeService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "查询文章类型和文章数量")
    @GetMapping("/articleCategory")
    @PreAuthorize(descs="文章类型查询文章类型和文章数量",name = "文章类型查询文章类型和文章数量",sn = "articleType:articleCategory")
    public R<List<ArticleCategory>> articleCategory(){
        try {
            return R.ok(articleTypeService.articleCategory());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    @ApiOperation(value = "查询归档数量")
    @GetMapping("/pigeonholeCount")
    @PreAuthorize(descs="文章类型查询归档数量",name = "文章类型查询归档数量",sn = "articleType:pigeonholeCount")
    public R<List<ArticleCategory>> pigeonholeCount(){
        try {
            return R.ok(articleTypeService.pigeonholeCount());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
}
